package com.amos.util;

import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright © 2018 嘉源锐信. All rights reserved.
 *
 * @Project: mu-exam
 * @ClassName: FieldUtils
 * @Package: com.amos.util
 * @author: zhuqb
 * @Description:
 * @date: 2020/3/11 0011 上午 10:51
 * @Version: V1.0
 */
public class FieldUtils {
    /**
     * 获取类的属性集合
     * @param clazz
     * @return
     */
    public static List<Field> getFields(Class<?> clazz) {
        if (StringUtils.isNullOrEmpty(clazz)) {
            return Collections.emptyList();
        }

        Field[] fields = clazz.getDeclaredFields();
        return Arrays.asList(fields);
    }

    /**
     * 获取类里面属性名称
     * @param clazz
     * @return
     */
    public static List<String> getFieldString(Class<?> clazz) {
        List<Field> list = getFields(clazz);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(Field::getName).collect(Collectors.toList());
    }
}
